package com.price.compare.common;

import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommonUtil {
    /**
     * 根据cls的ApiModelProperty 注解的notes属性获取表头
     *
     * @param cls
     * @return
     */
    public static List<Map<String, String>> getTitle(Class cls) {
        List<Map<String, String>> r = new ArrayList<>();
        List<Field> declaredFieldList = new LinkedList<>();
        Field[] declaredFields = cls.getDeclaredFields();
        Class tempClass = cls.getSuperclass();
        if (tempClass != null) {
            declaredFieldList.addAll(Arrays.asList(tempClass.getDeclaredFields()));
            declaredFieldList.addAll(Arrays.asList(declaredFields));
        }
        for (Field f : declaredFieldList) {
            ApiModelProperty annotation = f.getAnnotation(ApiModelProperty.class);
            if (annotation != null && StringUtils.isNotBlank(annotation.notes())) {
                Map<String, String> item = new HashMap<>(2);
                item.put("value", f.getName());
                item.put("title", annotation.notes());
                r.add(item);
            }
        }
        return r;
    }

    public static String notIn(String columnName, List ids) {
        StringBuilder strBu = new StringBuilder();
        strBu.append(columnName);
        strBu.append(" not in (");
        for (int i = 0; i < ids.size(); i++) {
            if (i != 0) {
                strBu.append(',');
            }
            strBu.append('\'');
            strBu.append(ids.get(i));
            strBu.append('\'');
        }
        strBu.append(')');
        return strBu.toString();
    }

    public static String in(String columnName, List ids) {
        StringBuilder strBu = new StringBuilder();
        strBu.append(columnName);
        strBu.append(" in (");
        for (int i = 0; i < ids.size(); i++) {
            if (i != 0) {
                strBu.append(',');
            }
            strBu.append('\'');
            strBu.append(ids.get(i));
            strBu.append('\'');
        }
        strBu.append(')');
        return strBu.toString();
    }


    public static boolean isNumeric(String str) {
        for (int i = str.length(); --i >= 0; ) {
            if (!Character.isDigit(str.charAt(i))) {
                if (str.charAt(i) != '.' && str.charAt(i) != ',' && str.charAt(i) != '-') {
                    if (!CommonUtil.isMatcher(str)){
                        return false;
                    }

                }
            }
        }
        return true;
    }


    public static  Boolean isMatcher(String number) {
        String regEx = "^([\\+|-]?\\d+(.{0}|.\\d+))[Ee]{1}([\\+|-]?\\d+)$";
        // 编译正则表达式
        Pattern pattern = Pattern.compile(regEx);
        // 忽略大小写的写法
        // Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(number);
        // 字符串是否与正则表达式相匹配
        boolean rs = matcher.matches();

        return rs ;
    }

    }
